package com.market.controller;

import com.market.common.exception.ControllerException;
import com.market.common.request.WorkerRequestParams;
import com.market.common.vo.ResponseCode;
import com.market.common.vo.ResponseResult;
import com.market.entity.Worker;
import com.market.service.WorkerService;

import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;

/**
 * @author :  KanShan
 * @version : 1.8
 * @Date: 2021/11/29/21:14
 * @注释 :
 */
@RestController
//@RequestMapping("/worker")
public class WorkerController {
    @Resource
    WorkerService workerService;

    /**
     * 根据员工名模糊查询员工信息
     *
     * @param name
     * @return
     */
    @GetMapping("/worker/listbyname")
    public ResponseResult<List<Worker>> selectListByName(String name) {
//        List<WorkerVo> workers = workerService.listWorkerByName();
        return ResponseResult.success(workerService.selectLisByName(name));
    }

    /**
     * 展示员工信息列表
     *
     * @return
     * @throws ControllerException
     */
    @GetMapping("/worker/find")
    public ResponseResult<List<Worker>> findWorker() throws ControllerException {
        List<Worker> worker = workerService.findWorker();
        return ResponseResult.success(ResponseCode.SUCCESS, worker);
    }

    /**
     * 注册功能
     *
     * @param worker
     * @return
     * @throws ControllerException
     */
    @PostMapping("/worker/insert")
    public ResponseResult<Integer> insertWorker(@RequestBody Worker worker) throws ControllerException {
        int i = workerService.register(worker);
        return ResponseResult.success(ResponseCode.SUCCESS, i);
    }

    /**
     * 删除员工信息
     *
     * @param id
     * @return
     * @throws ControllerException
     */
    @PostMapping("/worker/delete")
    public ResponseResult<Integer> deleteWorker(@RequestParam Long id) throws ControllerException {
        int i = workerService.deleteWorker(id);
        return ResponseResult.success(ResponseCode.SUCCESS, i);
    }

    /**
     * 修改员工密码
     *
     * @param workerId
     * @param password
     * @return
     * @throws ControllerException
     */
    @PostMapping("/worker/updatepassword")
    public ResponseResult<Integer> updateWorkerPassword(@RequestParam Integer workerId, @RequestParam String password) throws ControllerException {
        int i = workerService.updateWorkerPassword(workerId, password);
        return ResponseResult.success(ResponseCode.SUCCESS, i);
    }

    /**
     * 修改员工信息
     *
     * @param worker
     * @return
     * @throws ControllerException
     */
    @PostMapping("/worker/update")
    public ResponseResult<Integer> updateWorker(@RequestBody Worker worker) throws ControllerException {
        int i = workerService.updateWorker(worker);
        return ResponseResult.success(ResponseCode.SUCCESS, i);
    }

    /**
     * 登录功能
     *
     * @param workerId
     * @param password
     * @return
     * @throws ControllerException
     */
    @GetMapping("/worker/login")
    public ResponseResult<Worker> login(@RequestParam Long workerId, @RequestParam String password) throws ControllerException {
        Worker login = workerService.login(workerId, password);
        return ResponseResult.success(ResponseCode.SUCCESS, login);
    }
}
